<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!-- 1. 引入js -->
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
    <div id='app'>
        <!-- 5. 预留一个路由组件渲染的位置，当我们访问指定地址的时候，对应位置就加载对应的组件 -->
        <router-view></router-view>
    </div>

    <template id="listTemp">
        <div>
            <ul>
                <router-link tag="li" :to="'/detail?name='+item.name+'&id='+item.id" v-for="item in list">{{item.name}}</router-link>
            </ul>
        </div>
    </template>
    <script>
        const list = {
            template: `#listTemp`,
            data() {
                return {
                    list: [{
                        id: 1,
                        name: '李四'
                    },{
                        id: 2,
                        name: '王五'
                    },{
                        id: 3,
                        name: '张三'
                    },]
                }
            }
        }

        const detail = {
            template: `<div>详情组件:{{$route.query.name}}</div>`,
            created() {
                // 获取路由的信息
                console.log(this.$route.query);
            },
        }

        // 多一个VueRouter的构造器
        // 2. 实例化
        const routerIns = new VueRouter({
            // 3. 配置地址和组件的映射关系
            routes: [{
                    path: '/',
                    redirect: '/list'
                }, {
                    path: '/list',
                    component: list
                },
                {
                    path: '/detail',
                    component: detail
                }
            ]
        })
        const vm = new Vue({
            el: '#app',
            data: {},
            //4 .建立路由和vue实例的关系，把路由挂载在实例上
            router: routerIns
        })
    </script>
</body>

</html>